package com.qianyu.core.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.core.entity.base.BaseIdDto;
import com.qianyu.core.entity.bean.CronTask;
import com.qianyu.core.entity.dto.CornTaskDto;
import com.qianyu.core.helper.LoginHelper;
import com.qianyu.core.mapper.CronTaskMapper;
import com.qianyu.core.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CornTaskServiceImpl extends ServiceImpl<CronTaskMapper, CronTask> implements CornTaskService {

    private final UserService userService;

    private final GameAreaService gameAreaService;

    private final LinkService linkService;

    private final LinkActService linkActService;


    @Override
    public Page<CronTask> pageList(CornTaskDto.CornTaskPageDto dto) {
        Page<CronTask> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<CronTask> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CronTask::getUserId, LoginHelper.getUserId());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getExecStatus()), CronTask::getExecStatus, dto.getExecStatus());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getType()), CronTask::getType, dto.getType());
        return (Page<CronTask>) page(page, wrapper);
    }

    /**
     * 根据执行任务id获取详情
     *
     * @param dto
     */
    @Override
    public CronTask getInfo(BaseIdDto dto) {
        CronTask task = getById(dto.getId());
        task.setNickname(userService.getById(task.getUserId()).getNickname());
        task.setArea(gameAreaService.getById(task.getAreaId()).getArea());
        task.setTitle(linkService.getById(task.getLinkId()).getTitle());
        task.setActName(linkActService.getById(task.getActId()).getActName());
        return task;
    }
}
